package 书店销售管理系统;

import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class User extends JFrame {
    /**
         * 
         */
	
        private static final long serialVersionUID = 1L;
        private static final Integer WIDTH=600;
    private static final Integer HEIGHT=400;
        protected Object url;
        protected Object user;
        protected Object password;

    public User(){
        setTitle("登录界面");
        setSize(WIDTH,HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setLocationRelativeTo(null);

        ImageIcon imgBg= new ImageIcon("src/书店销售管理系统/登陆页面.jpg");
        JLabel jblBg = new JLabel(imgBg);//可插入背景图片
        jblBg.setBounds(0,0,WIDTH,HEIGHT);
        jblBg.setLayout(null);
        this.add(jblBg);

        //账号
        JLabel uid = new JLabel("Account：");
        uid.setBounds(150, 120, 110, 30);
        uid.setFont(new Font("PingFang SC", Font.BOLD, 17));
        uid.setForeground(Color.BLACK);
        jblBg.add(uid);
        //账号输入框
        JTextField uidInput = new JTextField();
        uidInput.setBounds(260, 120, 180, 30);
        jblBg.add(uidInput);

        //密码
        JLabel upwd=new JLabel("Password：");
        upwd.setBounds(150, 180, 110, 30);
        upwd.setFont(new Font("PingFang SC", Font.BOLD, 17));
        upwd.setForeground(Color.BLACK);
        jblBg.add(upwd);
        //密码输入框
        JPasswordField upwdInput = new JPasswordField();
        upwdInput.setBounds(260, 180, 180, 30);
        jblBg.add(upwdInput);

        //登录按钮
        JButton btnLogin=new JButton("Login");
        btnLogin.setBounds(270, 250, 110, 40);
        btnLogin.setBackground(Color.pink);
        btnLogin.setForeground(Color.WHITE);
        btnLogin.setFont(new Font("PingFang SC", Font.BOLD, 17));
        btnLogin.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                    Connection conn=null;
                    conn  = Oracleconnection.getConnection();
                String getInputUid=uidInput.getText();
                @SuppressWarnings("deprecation")
                                String getInputUpwd=upwdInput.getText();
                ResultSet rs=null;
                try {
                    String sql = "select * from USER1 where UNO =? and PASSWORD =?";
                    PreparedStatement ps = conn.prepareStatement(sql);
                    ps.setString(1,getInputUid);
                    ps.setString(2, getInputUpwd);
                    rs= ps.executeQuery();
                    //创建弹窗
                    JDialog dialog = new JDialog();
                    dialog.setLocationRelativeTo(null);
                    dialog.setSize(200,200);
                    if(rs.next()){
                        dialog.add(new JLabel("登录成功！"));
                    	setVisible(true);
                    	new Book().setVisible(true);
                    }else{
                        dialog.add(new JLabel("登录失败,输入账号或密码错误！"));
                    }
                    dialog.setVisible(true);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        });
        jblBg.add(btnLogin);
        setVisible(true);
    }
    //主函数只需运行loginFrame即可
    public static void main(String[] args) {
        new User();
    }
}